package com.itheima.test07;

import java.io.*;
import java.net.Socket;

//本地  读文件 向服务器上传  获取服务器的响应
public class ClientDemo {

    public static void main(String[] args) throws IOException {
        Runnable runnable = new Runnable() {
            public void run() {
                Socket socket = null;
                BufferedInputStream bis = null;
                try {
                    //创建 客户端对象
                    socket = new Socket("127.0.0.1", 8888);

                    //创建本地输入流
                    bis = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\沁儿.jpg"));
                    //获取 输出流
                    OutputStream os = socket.getOutputStream();
                    BufferedOutputStream bos = new BufferedOutputStream(os);
                    //边读边写
                    byte[] bytes = new byte[1024];
                    int len;
                    while ((len = bis.read(bytes)) != -1) {
                        bos.write(bytes, 0, len);
                    }
                    bos.flush();
                    //写完给一个结束标记
                    socket.shutdownOutput();

                    //获取输入流
                    InputStream is = socket.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));

                    //读取 服务器给的响应
                    String s;
                    while ((s = reader.readLine()) != null) {
                        System.out.println(s);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    //各种释放资源
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }


            }
        };
        for (int i = 0; i < 1000; i++) {

            new Thread(runnable).start();
        }
    }
}
